$(document).ready(function(){
    var userName = window.localStorage.getItem("gousername");
    $(".goUsername").html(userName);
    function rateEnterprise(){
        var selectList=$(".select-box");
        console.log(selectList)
        for(var i=0;i<selectList.length;i++){
            selectList[i].onchange=function(e){
                console.log(e);
                $.ajax({
                    type : "POST",
                    url:"http://59.110.114.107:8888/government/rateEnterprise",
                    data:{
                        userName:e.target.id,
                        creditRate:parseInt(e.target.value)
                    },
                    success:function (result) {
                        if(result.meta.success){
                            alert("评级成功！")
                        }
                    },
                    error:function () {
                        alert("错误");
                    }
                })
            }

        }
    }
    //var enterpriseNamArr = new Array()
    //---------------获取未评级的企业名-----------------
    // $("#ENameReq").click(function(){
    //     $("#tbody").html("");
    //     $.get("http://59.110.114.107:8888/government/enterpriseNameReq", function(result){
    //         if(result.meta.success){
    //             if(result.data.length == 0){
    //                 alert("无未评级企业信息！");
    //             }else{
    //                 for(var i= 0;i<result.data.length;i++){
    //                     var enterpriseName = result.data[i];
    //                     var esN = '<tr><td>'+enterpriseName
    //                         +'</td><td><select><option>1-A</option><option>2-AA</option><option>3-AAA</option></select></td><td><button  type="button" class="btn btn-default rate" >提交</button></td></tr>';
    //                     $("#tbody").append(esN);
    //                 }
    //             }
    //         }else{
    //             alert("无法访问！");
    //         }
    //     });
    // });

    //----------------点击评级，成功后删除该企业--------------------
    // $("#tbody").on("click",".rate",function(){
    //     var enterpriseName = $(this).parent().prev().prev().text();
    //     var creditRating = $(this).parent().prev().children(":first").val().substr(0,1);
    //     var deleteenterprise = $(this).parent().parent();
    //     $.ajax({
    //         type : "POST",
    //         url:"http://59.110.114.107:8888/government/creditRating",
    //         data:{
    //             enterpriseName:enterpriseName,
    //             creditRating:creditRating
    //         },
    //         success:function(result){
    //             alert(result.meta.message);
    //             deleteenterprise.remove();
    //
    //         },
    //         error:function(){
    //             alert("无法访问！");
    //         }
    //     });
    //
    // });
    var getUnratedEnterprise=function(){
        $('#ungraded').css({
            backgroundColor:'rgb(42,127,208)',
            color:'white'
        });
        $("#graded").css({
            backgroundColor:'#e6e6e6',
            color:'black'
        })
        $.ajax({
            type : "POST",
            url:"http://59.110.114.107:8888/government/getUnratedEnterprise",
            data:{
                pageNum:1,
                pageSize:1000
            },
            success:function(result){
                if(result.meta.success){
                    var data=result.data['未被评级的企业列表'];
                    $('#tbody').remove();
                    $('#rate-table').append('<tbody id="tbody"></tbody>');
                    var d=data.map(function (item,index) {
                        return '<tr><td></td><td>'+item.enterpriseName+'</td><td>'+item.socialCode+'</td><td>'+item.ownerName+'</td><td>'+
                            '<select id="'+item.socialCode+'" class="select-box"><option value="" style="display: none"></option><option value="1">A</option><option value="2">AA</option><option value="3">AAA</option><option value="4">AAAA</option><option value="5">AAAAA</option></select></td></tr>';

                    });
                    $('#tbody').append(d);
                    rateEnterprise();
                    var form = $(".form-horizontal");
                    $(form)[0].reset();
                }
            },
            error:function(){
                alert("错误");
            }
        });
    }

    $('#ENameReq').click(function () {
        getUnratedEnterprise();
    });
    $('#ungraded').click(getUnratedEnterprise);
    //---------获取已评级企业-------------
    $('#graded').click(function () {
        $('#graded').css({
            backgroundColor:'rgb(42,127,208)',
            color:'white'
        });
        $("#ungraded").css({
            backgroundColor:'#e6e6e6',
            color:'black'
        })
        $.ajax({
            type : "POST",
            url:"http://59.110.114.107:8888/government/getRatedEnterprise",
            data:{
                pageNum:1,
                pageSize:1000
            },
            success:function(result){
                if(result.meta.success) {
                    $('#tbody').remove();
                    $('#rate-table').append('<tbody id="tbody"></tbody>');
                    if (result.data) {
                        var data = result.data['已被评级的企业列表'];
                        var d = data.map(function (item, index) {
                            var num = item.CreditRate;
                            var str = '<tr><td></td><td>' + item.EnterpriseInfo.enterpriseName + '</td><td>' + item.EnterpriseInfo.socialCode + '</td><td>' + item.EnterpriseInfo.ownerName + '</td><td>' +
                                '<select id="' + item.EnterpriseInfo.socialCode + '" class="select-box"><option value="" style="display: none"></option><option value="1">A</option><option value="2">AA</option><option value="3">AAA</option><option value="4">AAAA</option><option value="5">AAAAA</option></select></td></tr>';
                            str=str.replace('<option value="' + num + '">', '<option value="' + num + '" selected="selected">')
                            return str;
                        });
                        $('#tbody').append(d);
                        rateEnterprise();
                        var form = $(".form-horizontal");
                        $(form)[0].reset();
                    } else {
                        console.log(result)
                    }
                }
            },
            error:function(){
                alert("错误");
            }
        });
    })
    //------------发布政策---------------
    $(".release1").click(function(){
        var policyName=$("#policy-name").val();
        var policyDesc=$("#policy-desc").val();
        console.log(policyName,policyDesc);
        $.ajax({
            type : "POST",
            url:"http://59.110.114.107:8888/government/releasePolicies",
            data:{
                userName:userName,
                policyName:policyName,
                policyDesc :policyDesc
            },
            success:function(result){
                if(result.meta.success==true){
                    alert("发布成功！");
                    var form = $(".form-horizontal");
                    $(form)[0].reset();
                }else{
                    alert("发布失败！");
                }
            },
            error:function(){
                alert("无法访问！");
            }
        });
    });
    //------------发布新闻---------------
    $(".release2").click(function(){
        var newName=$("#new-name").val();
        var newDesc=$("#new-desc").val();
        $.ajax({
            type : "POST",
            url:"http://59.110.114.107:8888/government/releaseNews",
            data:{
                userName:userName,
                newName:newName,
                newDesc:newDesc
            },
            success:function(result){
                if(result.meta.success==true){
                    alert("发布成功！");
                    var form = $(".form-horizontal");
                    $(form)[1].reset();
                }else{
                    alert("发布失败！");
                }
            },
            error:function(){
                alert("无法访问！");
            }
        });
    });
    //------------业务统计---------------
    $("#Buss").click(function () {
        $.ajax({
            type : "POST",
            url:"http://59.110.114.107:8888/government/getBusinessStatics",
            success:function(result){
                if(result.meta.success==true){
                    var data=result.data;
                    $("#Business-statistics #bankNum").text(data.BankNum);
                    $("#Business-statistics #productsNum").text(data.FinacialProductNum);
                    $("#Business-statistics #businessNum").text(data.EnterpriseNum);
                    $("#Business-statistics #requestNum").text(data.EnterpriseReqNum);
                    $("#Business-statistics #money").text(data.EnteriseReqCount);
                    $("#Business-statistics #applyProducts").text(data.ProductRequesNum);
                    $("#Business-statistics #loan").text(data.ProductRequesCount);
                    $("#Business-statistics #successProducts").text(data.ProductRequesSuccessNum);
                    $("#Business-statistics #turnovver").text(data.ProductRequesSuccessCount);
                    $("#Business-statistics #failProducts").text(data.ProductRequesFailNum);
                    $("#Business-statistics #unsoldAmount").text(data.ProductRequesFailCount);
                    $("#Business-statistics #dealProducts").text(data.ProductRequesProcessNum);
                    $("#Business-statistics #loanAmount").text(data.ProductRequesProcessCount);
                }else{
                    alert("查询失败！");
                }
            },
            error:function(){
                alert("无法访问！");
            }
        });
    })
    $(".myTabContent-title a").click(function () {
        sessionStorage.setItem("isLogin","true");
        window.location.href="index.html";
    })
    //------------退出-------------------
    $(".exit").click(function(){
        alert("退出登录！");
        window.localStorage.clear();
        window.sessionStorage.setItem("isLogin","false");
        window.location.href = "index.html";
    });
});